package forum;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Date;

public class PM implements Serializable{

	private int pmId;
	private int from;
	private String fromEmail;
	private String toEmail;
	private int to;
	private Timestamp date;
	private String message;
	
	public PM(int pmId, int from, int to, Timestamp date, String msg){
		this.pmId = pmId;
		this.from = from;
		this.to = to;
		this.date = date;
		this.message = msg;
		this.fromEmail = getEmailOfFrom();
		this.toEmail = getEmailOfTo();
	}
	
	public synchronized void Create(){
		
		Connection conn = db_code.connect();
		
		try {
			Statement stmt = conn.createStatement();
			String query = "INSERT INTO pm ( toUserID, fromUserID, date, message)" +
					"VALUES(" + 
						to + "," + 
						from + ",'" + 
						date + "','" + 
						message + "');";
			
			stmt.executeUpdate(query,Statement.RETURN_GENERATED_KEYS);
			
			ResultSet rs = stmt.getGeneratedKeys();
			if ( rs.next() ) {
			    int key = rs.getInt(1);
			    setPmId(key);
			}
			db_code.disconnect(conn);
		} catch (Exception e) {
			db_code.disconnect(conn);
			System.out.println("FAIL! INSERT!");
			e.printStackTrace();
		}
	
	}
	
	public synchronized void Edit(){
		Connection conn = db_code.connect();
		try {
			
			Statement stmt = conn.createStatement();
			String query = "UPDATE pm " +
					"SET " +
						"from = " + from + "," +
						"to = " + to + ","+
						"date = '" + date + "',"+
						"message = '" + message + "'"+
					"WHERE " +
						"pmID = " + pmId;
			
			stmt.execute(query);
			db_code.disconnect(conn);
		} catch (Exception e) {
			db_code.disconnect(conn);
			System.out.println("FAIL! UPDATE!");
			e.printStackTrace();
		}
		
	}
	
	public synchronized void Delete(){
		Connection conn = db_code.connect();
		try {
			
			Statement stmt = conn.createStatement();
			String query = "DELETE FROM pm WHERE pmID = " + pmId;
			
			stmt.execute(query);
			db_code.disconnect(conn);
		} catch (Exception e) {
			db_code.disconnect(conn);
			System.out.println("FAIL! DELETE!");
			e.printStackTrace();
		}
		
	}
	
	
	public String getEmailOfFrom(){
		Connection conn = db_code.connect();
		try {
			
			Statement stmt = conn.createStatement();
			String query = "SELECT email FROM user WHERE userID = "+from+" LIMIT 1;";
			
			ResultSet rs = stmt.executeQuery(query);
			if ( rs.next() ) {
			    String email = rs.getString(1);
			    db_code.disconnect(conn);
			    return email;
			}
			db_code.disconnect(conn);
		} catch (Exception e) {
			db_code.disconnect(conn);
			System.out.println("FAIL! GetEmailFrom!");
			e.printStackTrace();
		}
		
		return "";
	}
	
	public String getEmailOfTo(){
		Connection conn = db_code.connect();
		try {
			
			Statement stmt = conn.createStatement();
			String query = "SELECT email FROM user WHERE userID = "+to+" LIMIT 1;";
			
			ResultSet rs = stmt.executeQuery(query);
			if ( rs.next() ) {
			    String email = rs.getString(1);
			    db_code.disconnect(conn);
			    return email;
			}
			db_code.disconnect(conn);
		} catch (Exception e) {
			db_code.disconnect(conn);
			System.out.println("FAIL! GetEmailTo!");
			e.printStackTrace();
		}
		return "";
	}
	
	
	public String ToString(){
		return "pmId:" + pmId +
				"from: " + from +
				", to: " + to +
				", msg: " + message +
				", date: " + date;
	}
	
	public int getFrom() {
		return from;
	}
	public void setFrom(int from) {
		this.from = from;
	}
	public int getTo() {
		return to;
	}
	public void setTo(int to) {
		this.to = to;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Timestamp date) {
		this.date = date;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}

	public int getPmId() {
		return pmId;
	}

	public void setPmId(int pmId) {
		this.pmId = pmId;
	}

	public String getFromEmail() {
		return fromEmail;
	}

	public void setFromEmail(String fromEmail) {
		this.fromEmail = fromEmail;
	}

	public String getToEmail() {
		return toEmail;
	}

	public void setToEmail(String toEmail) {
		this.toEmail = toEmail;
	}
}
